package yf.mall.controller;

import java.util.*;

import com.yf.response.RRException;
import com.yf.utils.ShiroUtils;
import org.apache.poi.hssf.record.DVALRecord;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;

import com.yf.consts.DaoConst;
import yf.mall.common.Const;
import yf.mall.dao.LotteryActDao;
import yf.mall.dao.MallGoodsDao;
import yf.mall.dao.MallStoreDao;
import yf.mall.dao.SysUserUserDao;
import yf.mall.entity.*;
import yf.mall.enums.MallActStatusEnum;
import yf.mall.enums.MallGoodsShelfEnum;
import yf.mall.service.*;
import com.yf.response.R;


/**
 * 活动Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Nov 25 13:53:21 CST 2019
 */
@RestController
@RequestMapping("/mall/act")
public class MallActController {
    @Autowired
    private MallActService mallActService;

    @Autowired
    private MallGoodsDao mallGoodsDao;

    @Autowired
    private LevelService levelService;
    @Autowired
    private LotteryActDao lotteryActDao;
    @Autowired
    private ShopService shopService;


//    /**
//     * 列表活动
//     */
//    @RequestMapping("/list")
//    @RequiresPermissions("mall:act:list")
//    public R list(@RequestParam Map<String, Object> params) {
//        Map<String, Object> paramMap = new HashMap<>();
//
//        PageHelper.startPage(DaoConst.getPage(params), DaoConst.getLIMIT(params));
//        List<MallActEntity> mallActList = null;
//        //根据产品名查询 que
//        if (params.get("goodsName") != null && !"".equals(params.get("goodsName"))) {
//            String goodsName = (String) params.get("goodsName");
//            String actName = null;
//            Integer status = null;
//            if (params.get("name") != null && !"".equals(params.get("name"))) {
//                actName = (String) params.get("name");
//            }
//            if (params.get("status") != null && !"".equals(params.get("status"))) {
//                status = Integer.parseInt((String) params.get("status"));
//            }
//            mallActList = mallActService.findByGoodsName(goodsName, actName, status);
//        } else {
//            mallActList = mallActService.queryList(params);
//        }
//        List<MallGoodsEntity> goodsEntities = goodsService.queryList(null);
//        List<LotteryActEntity> lotteryActEntities = lotteryActDao.findAct();
//        List<LevelEntity> levelEntities = levelService.queryList(null);
//        return R.ok(new PageInfo(mallActList)).putExtra("goodsList", goodsEntities).putExtra("levelList", levelEntities).putExtra("lotteryList", lotteryActEntities);
//    }

    /**
     * 列表活动
     */
    @RequestMapping("list")
    @RequiresPermissions("mall:act:list")
    public R list(@RequestParam Map<String, Object> params) {

        PageHelper.startPage(DaoConst.getPage(params), DaoConst.getLIMIT(params)).setOrderBy((String) params.get("order"));

        List<MallActEntity> actEntityList = mallActService.list(params);
        for (MallActEntity mallActEntity : actEntityList) {
            MallGoodsEntity goodsEntity = mallGoodsDao.get(mallActEntity.getGoodsId());
            if (goodsEntity.getUserId() != null && !goodsEntity.getUserId().equals(Const.ADMIN_ID)) {
                ShopEntity shop = shopService.findByUserId(goodsEntity.getUserId());
                mallActEntity.setShopName(shop.getName());
            } else {
                mallActEntity.setShopName("社集优选自营店");
            }
        }
        List<LotteryActEntity> lotteryActEntities = lotteryActDao.findAct();
        List<LevelEntity> levelEntities = levelService.queryList(null);
        List<MallGoodsEntity> goodsEntities = mallGoodsDao.queryList(null);

        List<ShopEntity> shops = shopService.findAll();
        return R.ok(new PageInfo(actEntityList)).putExtra("shops", shops)
                .putExtra("levelList", levelEntities)
                .putExtra("goodsList", goodsEntities)
                .putExtra("lotteryList", lotteryActEntities);
    }

    /**
     * 查看活动
     */
    @SysLog("查看活动")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:act:info")
    public R info(@PathVariable("id") Long id) {
        MallActEntity mallAct = mallActService.get(id);
        MallGoodsEntity goodsEntity = mallGoodsDao.get(mallAct.getGoodsId());
        mallAct.setGoodsName(goodsEntity.getName());

        return R.ok(mallAct);
    }

    @Autowired
    private MallStoreDao mallStoreDao;

    /**
     * 保存活动
     */
    @SysLog("保存活动")
    @RequestMapping("/save")
    @RequiresPermissions("mall:act:save")
    public R save(MallActEntity mallAct) {
        //如果该产品存在已经开始的活动 则不允许在添加
        Long goodsId = mallAct.getGoodsId();
        MallActEntity mallActEntity = mallActService.findByGoodsIdAndstatus(goodsId, MallActStatusEnum.OPEN.getIndex());
        if (mallActEntity != null) {
            throw new RRException("该产品存在进行中的活动");
        }
        mallActService.save(mallAct);
        return R.ok(mallAct);
    }

    /**
     * 修改活动
     */
    @SysLog("修改活动")
    @RequestMapping("/update")
    @RequiresPermissions("mall:act:update")
    public R update(MallActEntity mallAct) {
        mallActService.update(mallAct);
        MallGoodsEntity goodsEntity = mallGoodsDao.get(mallAct.getGoodsId());
        if (goodsEntity.getUserId() != null && !goodsEntity.getUserId().equals(Const.ADMIN_ID)) {
            ShopEntity shopEntity = shopService.findByUserId(goodsEntity.getUserId());
            mallAct.setShopName(shopEntity.getName());
        } else {
            mallAct.setShopName("社集优选自营店");
        }

        return R.ok(mallAct);
    }

    /**
     * 删除活动
     */
    @SysLog("删除活动")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:act:delete")
    public R delete(Long[] ids) {
        mallActService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 所有活动
     */
    @SysLog("所有活动")
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MallActEntity> mallActList = mallActService.queryList(params);

        return R.ok(mallActList);
    }

    /**
     @ModelAttribute public SearchParam<MallActEntity> searchParam(SearchParam<MallActEntity> params, MallActEntity mallAct) {
     if (mallAct != null) {
     params.setEntity(mallAct);
     }

     return params;
     }
     **/
    /***********自定义开始***********/

    @Autowired
    private SumService sumService;

    @RequestMapping("/sum/top")
    public R sumTop(Long actId, Integer limit) {
        List<Object[]> data = sumService.sumTopUserBuy(actId, limit);
        return R.ok(data);
    }

    @RequestMapping("/findByUserId/{userId}")
    public R findByUserId(@PathVariable Long userId) {
        List<MallGoodsEntity> goodsEntityList = null;
        if (userId.equals(Const.ADMIN_ID)) {
            goodsEntityList = mallGoodsDao.findUserIdIsNull(null);
        } else {
            goodsEntityList = mallGoodsDao.findByUserId(userId, null);
        }

        return R.ok(goodsEntityList);
    }

    /**
     * 活动解绑
     *
     * @param actId
     * @return
     */
    @RequestMapping("/untieAct/{actId}")
    public R untieAct(@PathVariable Long actId) {
        mallActService.untieAct(actId, MallActStatusEnum.CLOSED.getIndex());
        return R.ok();
    }

    /**
     * 查找产品
     *
     * @return
     */
    @RequestMapping("/findByGoodsIdOrUserId")
    public R findByGoodsId(Long goodsId) {

        MallGoodsEntity goodsEntity = mallGoodsDao.get(goodsId);
        if (goodsEntity.getUserId() == null || goodsEntity.getUserId().equals(Const.ADMIN_ID)) {
            goodsEntity.setShopName("社集优选自营店");
        } else {
            ShopEntity shopEntity = shopService.findByUserId(goodsEntity.getUserId());
            goodsEntity.setShopName(shopEntity.getName());
        }
        return R.ok(goodsEntity);
    }
}
